﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using CNdotnet.Ezsocio.Core.Service;
using CNdotnet.Ezsocio.Core.DataEntity;
using CNdotnet.Ezsocio.DataEntities;
using CNdotnet.Ezsocio.DataProvider;
using CNdotnet.Ezsocio.EmailService;
using CNdotnet.Ezsocio.Core.Common;
using CNdotnet.Ezsocio.BusinessObjects;
using CNdotnet.Ezsocio.TemplateEngine;

namespace CNdotnet.Ezsocio.BusinessServices
{
    public class FriendService : ServiceBase
    {
        public void Invite(int currentUserID,string name, string email) {
            VerifyCode code = new VerifyCode { 
                CodeType = 3,
                Code = Guid.NewGuid().ToString(),
                CreateDate = DateTime.Now,
                SendDate = DateTime.Now,
                ExpireDate = DateTime.Now.AddDays(30),
                UserId =currentUserID
            };

            LoadRepository<IVerifyCodeRepository>().Create(code);

            TemplateContract template = new TemplateContract("InviteTemplate");
            template.Parameters.Add("Name", name);
            template.Parameters.Add("URL", String.Format("http://www.CNdotnet.Ezsocio.com/Account/RegVerify/{0}", code.Code));
            template.Parameters.Add("Team", "CNdotnet.Ezsocio.NET Team");

            MailContract mail = new MailContract() {
                Category=MailCategory.Invite,
                Content = template,
                CreateDate=DateTime.Now,
                FromAddress = AppSettings.FromEmailAddress,
                Priority = MailPriority.Normal,
                Recipient= email,
                Retries = 5,
                Subject="Invite you to register CNdotnet.Ezsocio.NET"
            };
            MailHandler ms = new MailHandler();
            ms.Send(mail);            
        }

        public void AddFriend(int userID,int friendID,int groupID) {
            if (Exist(userID, friendID)) {
                return;
            }
            Friendship fs = new Friendship();
            fs.UserId = userID;
            fs.FriendId = friendID;
            fs.GroupId = groupID;
            fs.ConfirmDate = DateTime.Now;
            fs.CreateDate = DateTime.Now;
            fs.IsConfirmed = false;
            fs.Note = "";
            LoadRepository<IFriendRepository>().Insert(fs);
        }

        public bool Exist(int userID, int friendID) {
            return LoadRepository<IFriendRepository>().Exist(userID, friendID);
        }

        public List<AccountContract> GetFriendsByID(int groupID)
        {
            List<AccountContract> accounts = new List<AccountContract>();
            DataLoadOptions option = new DataLoadOptions();
            option.LoadWith<FriendGroup>(friendGroup => friendGroup.Friendships);
            option.LoadWith<Friendship>(friendShip => friendShip.FriendUser);
            option.LoadWith<User>(user => user.Profile);
            FriendGroup group = LoadRepository<IFriendGroupRepository>().GetSingle(groupID);
            foreach (var f in group.Friendships)
            {
                if (f.IsConfirmed) {
                    AccountContract ac = new AccountContract();
                    ac = AccountService.GetAccount(f.FriendUser,false);
                    accounts.Add(ac);
                }
            }
            return accounts;
        }
        /// <summary>
        /// get friend users with filter
        /// </summary>
        /// <param name="userID">user id</param>
        /// <param name="kw">keyword</param>
        /// <returns></returns>
        public List<AccountContract> GetFriendsByUserID(int userID,string kw)
        {
            var accounts = new List<AccountContract>();
            var friends = LoadRepository<IFriendRepository>().GetFriends(userID, kw);
            foreach (var f in friends)
            {
                if (f.IsConfirmed) {
                    AccountContract ac = AccountService.GetAccount(f.FriendUser,false);
                    accounts.Add(ac);
                }
            }
            return accounts;
        }
        /// <summary>
        /// get friend users who are online
        /// </summary>
        /// <param name="userID">user id</param>
        /// <param name="kw">keyword</param>
        /// <returns></returns>
        public List<AccountContract> GetOnLineFriendsByUserID(int userID, string kw) {
            var accounts = new List<AccountContract>();
            var friends = LoadRepository<IFriendRepository>().GetFriends(userID, kw);
            foreach (var f in friends) {
                if (f.IsConfirmed&&f.FriendUser.IsOnline) {
                    AccountContract ac = AccountService.GetAccount(f.FriendUser,false);
                    accounts.Add(ac);
                }
            }
            return accounts;
        }
        /// <summary>
        /// get friend users who are online
        /// </summary>
        /// <param name="userID">user id</param>
        /// <param name="kw">keyword</param>
        /// <returns></returns>
        public List<AccountContract> GetFriendsWhoHasStatusSetting(int userID, string kw) {
            var accounts = new List<AccountContract>();
            var friends = LoadRepository<IFriendRepository>().GetFriends(userID, kw);
            foreach (var f in friends) {
                if (f.IsConfirmed && !string.IsNullOrEmpty(f.FriendUser.Profile.State)) {
                    AccountContract ac = AccountService.GetAccount(f.FriendUser,false);
                    accounts.Add(ac);
                }
            }
            return accounts;
        }
        /// <summary>
        /// delete the relations of between userid and friendid
        /// </summary>
        /// <param name="userID">one user</param>
        /// <param name="friend">anthoer user </param>
        public void Delete(int userID, int friend)
        {
            LoadRepository<IFriendRepository>().Delete(userID, friend);
        }

        public void UpdateGroupSetting(int userID,int friendID, int newGroupID)
        {
            LoadRepository<IFriendRepository>().UpdateGroupSetting(userID, friendID,newGroupID);
        }
        /// <summary>
        /// allow the request of add me to one's friend list
        /// </summary>
        /// <param name="msgID">message id</param>
        /// <param name="userID"></param>
        /// <param name="friendID"></param>
        /// <param name="groupID"></param>
        /// <param name="note"></param>
        public void Through(int userID, int friendID, int groupID, string note) {
            LoadRepository<IFriendRepository>().Through(userID, friendID,groupID,note);            
        }
    }
}
